#include "stdafx.h"
#include "LogTag.h"

#pragma region Open Tag File
/// <summary>
/// Open a file and return a handle to the file.
/// <summary>
__declspec(dllexport) int __stdcall OpenTagFile(const char *fileName)
{
	int fileHandle = NULL;
	
	// attempt to open the file
	LogFile *logFile = new LogFile();
	logFile->SetFileName(fileName);

	// store the file in memory for later calls
	if (logFile->OpenFile())
	{
		if (logFile->LoadData())
			fileHandle = AddLogFile(logFile);
	}

	return fileHandle;
}
#pragma endregion

#pragma region Get Reading Count
/// <summary>
/// Get the number of readings.
/// <summary>
__declspec(dllexport) int __stdcall GetReadingsCount(int handle)
{
	LogFile *logFile = GetLogFile(handle);
	if (logFile != NULL)
	{
		return logFile->GetReadingCount();
	}

	return -1;
}
#pragma endregion

#pragma region Get Reading
/// <summary>
/// Get the values of a reading.
/// <summary>
__declspec(dllexport) bool __stdcall GetReading(int handle, int index, Reading *reading)
{
	LogFile *logFile = GetLogFile(handle);
	if (logFile != NULL)
	{
		// get the reading
		LOGTAG_READING tagReading = logFile->GetReadingByIndex(index);
		
		// get the time taken
		reading->Taken.Year = tagReading.stTaken.wYear;
		reading->Taken.Month = tagReading.stTaken.wMonth;
		reading->Taken.Day = tagReading.stTaken.wDay;

		// get the readings
		reading->Humidity = (float)tagReading.dReading[0];
		reading->Temperature = (float)tagReading.dReading[1];

		return true;
	}

	return false;
}
#pragma endregion